package com.tl.sun.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;

import com.blankj.utilcode.utils.LogUtils;
import com.orhanobut.logger.Logger;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.modelmsg.SendAuth;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.tl.sun.common.ClientConfig;
import com.tl.sun.event.LoginEvent;
import com.tl.sun.event.ShareEvent;
import com.tl.sun.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;

/**
 * Created by bin on 16/8/21.
 */
public class WXEntryActivity extends Activity implements IWXAPIEventHandler {

    private static final int TIMELINE_SUPPORTED_VERSION = 0x21020001;
    private static final int RETURN_MSG_TYPE_LOGIN = 1;
    private static final int RETURN_MSG_TYPE_SHARE = 2;
    private Button gotoBtn, regBtn, launchBtn, checkBtn;

    // IWXAPI 是第三方app和微信通信的openapi接口
    private IWXAPI api;
    private int code;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.entry);

        // 通过WXAPIFactory工厂，获取IWXAPI的实例
        api = WXAPIFactory.createWXAPI(this.getApplicationContext(), ClientConfig.WX_APP_ID, false);

        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

        setIntent(intent);
        api.handleIntent(intent, this);
    }

    // 微信发送请求到第三方应用时，会回调到该方法
    @Override
    public void onReq(BaseReq req) {
        Logger.e("slsosgnwl");
//        switch (req.getType()) {
//            case ConstantsAPI.COMMAND_GETMESSAGE_FROM_WX:
//                goToGetMsg();
//                break;
//            case ConstantsAPI.COMMAND_SHOWMESSAGE_FROM_WX:
//                goToShowMsg((ShowMessageFromWX.Req) req);
//                break;
//            default:
//                break;
//        }
    }

    // 第三方应用发送到微信的请求处理后的响应结果，会回调到该方法
    @Override
    public void onResp(BaseResp resp) {
        switch (resp.errCode) {
            case BaseResp.ErrCode.ERR_OK:
                switch (resp.getType()) {
                    case RETURN_MSG_TYPE_LOGIN:
                        String wxCode = ((SendAuth.Resp) resp).code;
                        LogUtils.e("微信Code:" + wxCode);
//                        ToastUtil.showToast("获取信息成功");
                        EventBus.getDefault().post(new LoginEvent(wxCode));
                        break;
                    case RETURN_MSG_TYPE_SHARE:
                        EventBus.getDefault().postSticky(new ShareEvent(0));
                        break;
                }
                break;
            case BaseResp.ErrCode.ERR_USER_CANCEL:
                EventBus.getDefault().postSticky(new ShareEvent(1));
                ToastUtil.showToast("用户取消");

                break;
            case BaseResp.ErrCode.ERR_AUTH_DENIED:
//                result = R.string.errcode_deny;
                EventBus.getDefault().postSticky(new ShareEvent(1));
                ToastUtil.showToast("认证失败");
                break;

        }

        finish();
    }


}
